<?php

namespace app\common\logic;

use think\Db;
use think\Model;

class FullRecord extends Model
{
    protected $table = 'sys_v_all_record';

    protected function getTimeAttr($time)
    {
        return date('H:i:s', $time);
    }

    protected function getDateAttr($time)
    {
        return date('Y-m-d', $time);
    }

    /**
     * 返回指定日期最早到岗列表
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getEarliests($where = [])
    {
        $ret = self::where($where)->field('avatar,name,dept,unix_time time')->group('name')->order('unix_time')->select();
        return $ret;
    }

    /**
     * 返回指定日期前一天最晚离岗列表
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getLatests($where = [])
    {
        $ret = self::where($where)->field('avatar,name,max(unix_time) time,unix_time date,dept')->group('name')->order('max(unix_time) desc')->select();
        return $ret;
    }

    /**
     * 返回指定日期前一天打卡异常列表
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getExceptions($where = [])
    {
        $ret = self::where($where)
            ->field('avatar,name,dept,userId,count(name) count')->group('name')->order('count(name) desc')->having('count%2=1')->select();
        return $ret;
    }

    /**
     * 返回指定日期所有人打卡记录
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getRecordByTime($where = [])
    {
        $ret = self::where($where)->field('userId,name,dept,unix_time time,time full_time')->order('unix_time desc')->select();
        return $ret;
    }

    /**
     * 根据unix时间段返回每一天的打卡次数和时间列表
     * @param $time
     * @param $dept_id
     * @return string
     * @author wb <pithyone@vip.qq.com>
     */
    public static function getSoftRecordByTime($time, $dept_id)
    {
        $rows = empty(input('rows')) ? config('paginate.list_rows') : input('rows');

        $where['unix_time'] = ['between', $time];
        $where['depart_id'] = ['in', Department::getChildIds($dept_id)];

        $list = self::where($where)->field("userId,name,depart_id,dept,COUNT(name) counts,GROUP_CONCAT(unix_time ORDER BY unix_time ASC) log,LEFT(time,10) dates")
            ->group('name,LEFT(time,10)')->order('userId')->paginate($rows);
        $ret = $list->render();
        return $ret;
    }

    public static function getRecordByUidANDTime($uid, $time)
    {
        $where['userid'] = $uid;
        $where['unix_time'] = ['between', $time];

        $ret = self::where($where)->field("userId,name,depart_id,dept,COUNT(name) counts,GROUP_CONCAT(unix_time ORDER BY unix_time ASC) log,LEFT(time,10) dates")
            ->group('name,LEFT(time,10)')->order('userId')->select();
        return $ret;
    }

    //工时查询
    public static function SoftRecordByTime($time, $dept_id)
    {
        $where['depart_id'] = ['in', $dept_id];
        $where['unix_time'] = ['between', [$time["start_time"], $time["end_time"]]];
        $ret = self::where($where)->field("userId,name,avatar,dept,GROUP_CONCAT(unix_time ORDER BY unix_time ASC ) log")
            ->group("name,LEFT(time,10)")->order("userId")->select();
        return $ret;
    }

    //迟到查询
    public static function late($time)
    {
        $where['depart_id'] = 3;
        $where['unix_time'] = ['between', [$time["start_time"], $time["end_time"]]];
        $ret = self::where($where)->field("userId,name,avatar,dept,GROUP_CONCAT(right(time,8)) log")->group("name,left(time,10)")->select();
        return $ret;
    }

    //补签查询
    public static function buqian($time)
    {
        $where['create_time'] = ['between', [$time["start_time"], $time["end_time"]]];
        $where['status'] = 1;
        $ret = Db::table("sys_kq_buqian")->field("uid,name,avatar,dept,count(name) count")->where($where)->group("uid")->order('count desc')->select();
        return $ret;
    }

    /**
     * 返回指定日期前一天最晚离岗
     * @param array $where
     * @return false|\PDOStatement|string|\think\Collection
     */
    public static function getLastLeave($start, $end)
    {

        $sql = "select a.name,a.avatar,left(a.time,10) as datatime ,right(a.time,8) as hourtime from sys_v_all_record a,
              (select date(time) time,max(time) max_datetime 
                from sys_v_all_record 
                where unix_time 
                between " . $start . ' and ' . $end . "
                group by date(time)) b 
                where a.time=b.max_datetime and date(a.time)=b.time order by datatime desc";
        $where = self::query($sql);
        return $where;

    }

    public static function change($seconds)
    {
        if ($seconds > 3600) {
            $hours = intval($seconds / 3600);
            $minutes = $seconds % 3600;
            $time = $hours . ":" . gmstrftime('%M:%S', $minutes);
        } else {
            $time = gmstrftime('%H:%M:%S', $seconds);
        }
        return $time;
    }
}